package com.example.springaidemo.service;

import com.example.springaidemo.model.ChatMessage;
import com.example.springaidemo.model.ChatSession;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.List;

@Service
public class ChatSessionService {
    private final Map<String, ChatSession> sessions = new ConcurrentHashMap<>();

    // 创建新会话
    public ChatSession createSession() {
        ChatSession session = new ChatSession();
        sessions.put(session.getId(), session);
        return session;
    }

    // 获取会话
    public ChatSession getSession(String sessionId) {
        return sessions.get(sessionId);
    }

    // 添加消息到会话
    public void addMessageToSession(String sessionId, String role, String content) {
        ChatSession session = getSession(sessionId);
        if (session != null) {
            session.addMessage(new ChatMessage(role, content));
        }
    }

    // 获取会话中的消息列表
    public List<ChatMessage> getMessagesFromSession(String sessionId) {
        ChatSession session = getSession(sessionId);
        if (session != null) {
            return session.getMessages();
        }
        return List.of();
    }

    // 删除会话
    public void deleteSession(String sessionId) {
        sessions.remove(sessionId);
    }
}